<?php

namespace common\widgets;

use yii\bootstrap\ButtonGroup;
use yii\bootstrap\Widget;
use yii\helpers\Html;

/**
 * This is just an example.
 */
class Box extends Widget
{
    const TYPE_INFO = 'info';
    const TYPE_PRIMARY = 'primary';
    const TYPE_SUCCESS = 'success';
    const TYPE_DEFAULT = 'default';
    const TYPE_DANGER = 'danger';
    const TYPE_WARNING = 'warning';
    /**
     * @var string $type color style of widget*
     */
    public $type = self::TYPE_DEFAULT;
    /**
     * @var boolean $solid is solid box header*
     */
    public $solid = false;
    /**
     * @var boolean $withBorder add border after box header (for AdminLte 2.0)*
     */
    public $withBorder = false;
    /**
     * @var string $tooltip box -tooltip*
     */
    public $tooltip = '';
    /**
     * @var string $tooltipPlacement -top/bottom/left/or right *
     */
    public $tooltipPlacement='bottom';
    /**
     * @var string $title *
     */
    public $title = '';
    /**
     * @var string $footer *
     */
    public $footer = '';
    /**
     * @var boolean $collapse show or not Box - collapse button*
     */
    public $collapse = false;

    public $collapseDefault = false;
    /**
     * @var boolean $collapseRemember - set cookies for rememer collapse stage*
     */
    public $collapseRemember = true;
    /**
     * @var string|array $customTools code of custom box toolbar - string html code, or array for yii\bootstrap\ButtonGroup $buttons option*
     */
    public $customTools = '';
    /**
     * @var string|array $leftTools code of custom box toolbar in left corner  - string html code, or array for yii\bootstrap\ButtonGroup $buttons option*
     */

    public $leftTools = '';
    public $headerTag='h3';
    public $bodyOptions;
    public $buttonsTemplate;
    public $grid;
    public $content;
    private $_cid = null;
    public function init()
    {
        if (!isset($this->options['id'])) {
            $this->_cid =$this->options['id'] = 'bc_'.$this->getId();
        }
        Html::addCssClass($this->options,'box');
        Html::addCssClass($this->options,'box-' . $this->type);
        if($this->solid){
            Html::addCssClass($this->options,'box-solid');
        }
        if($this->collapse and $this->collapseDefault and !$this->collapseRemember){
            Html::addCssClass($this->options,'collapsed-box');
        }
        if(is_array($this->customTools)){
            if($this->collapse){
//                $this->customTools[]='<button class="btn btn-'.$this->type.' btn-xs" data-widget="collapse" id="'. $this->_cid . '_btn"><i class="fa fa-minus"></i></button>';
                $this->customTools[]='<button class="btn btn-box-tool" data-widget="collapse" id="'. $this->_cid . '_btn"><i class="fa fa-minus"></i></button>';
            }
            $this->customTools=ButtonGroup::widget([
                'buttons'=>$this->customTools,
                'encodeLabels'=>false
            ]);
        }else{
            $this->customTools=$this->customTools.($this->collapse?
//                    '<button class="btn btn-'.$this->type.' btn-xs" data-widget="collapse" id="'. $this->_cid . '_btn"><i class="fa fa-minus"></i></button>'
                    '<button class="btn btn-box-tool" data-widget="collapse" id="'. $this->_cid . '_btn"><i class="fa fa-minus"></i></button>'
                    :'');
        }
        if(is_array($this->leftTools) && !empty($this->leftTools)){
            $this->leftTools=ButtonGroup::widget([
                'buttons'=>$this->leftTools,
                'encodeLabels'=>false
            ]);
        }
        $custTools=Html::tag('div',$this->customTools,['class'=>'box-tools pull-right']);
        $headerContent=(!$this->leftTools?'':'<div class="box-tools pull-left">'.$this->leftTools.'</div>');
        $headerContent.=(!$this->title ? '' : Html::tag($this->headerTag,$this->title,['class'=>'box-title']));
        $headerContent.=($this->customTools||$this->collapse)?$custTools:'';
        $headerOptions=['class'=>'box-header'];
        if($this->withBorder){
            Html::addCssClass($headerOptions,'with-border');
        }
        if($this->tooltip){
            $headerOptions=array_merge($headerOptions,[
                'data-toggle'=>'tooltip',
                'data-original-title'=>$this->tooltip,
                'data-placement'=>$this->tooltipPlacement
            ]);
        }
        $header=Html::tag('div',$headerContent,$headerOptions);
        echo '<div '.Html::renderTagAttributes($this->options).'>'
            . (!$this->title && !$this->collapse && !$this->customTools && !$this->leftTools
                ? ''
                : $header)
            . '<div class="box-body">';

        if($this->content){
            echo $this->content;

        }

    }
    public function run()
    {
        echo '</div>'
            . (!$this->footer ? '' : "<div class='box-footer'>" . $this->footer . "</div>")
            . '</div>';
    }

}
